package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportNonrecurring;
import com.simon.annual.entity.ReportRetroactive;
import com.simon.annual.repositoy.IReportNonrecurringRepositoy;
import com.simon.annual.repositoy.IReportRetroactiveRepositoy;
import com.simon.annual.service.IAccountingAndFinance;
import com.simon.annual.service.ICompanyPublicInfo;

/**
 * 第三节：会计数据和财务指标摘要
 * */
@Service
public class FindAccountingAndFinanceImpl implements IAccountingAndFinance {

	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportNonrecurringRepositoy reportNonrecurringRepositoy;
	@Resource
	private IReportRetroactiveRepositoy reportRetroactiveRepositoy;
	@Resource
	private ICompanyPublicInfo companyPublicInfo;
	@Resource
	private Environment env;
	
	@Override
	public void analysis(Document doc, int companyPtrId) {
		
		Elements ele = doc.getElementsMatchingOwnText(env.getProperty("pdf.three.non"));
		//非经常性损益
//		Element nonrecurring = companyPublicInfo.findNextTag("table", ele.first());
		if(ele.last()!=null){
			Element nonrecurring = ele.last().nextElementSibling();
			List<ReportNonrecurring> list =new ArrayList<ReportNonrecurring>();
			Elements nonrecurringTr = nonrecurring.select("table tr");
			for (int i = 1; i < nonrecurringTr.size(); i++) {
				ReportNonrecurring non = new ReportNonrecurring();
				Elements tds = nonrecurringTr.get(i).children();
				if(tds.size()==2){
					non.setName(tds.get(0).text());
					non.setValue(tds.get(1).text());
					non.setProjectScopeId(companyPtrId);
					list.add(non);
				}
			}
			reportNonrecurringRepositoy.save(list);
			//会计政策变更及会计差错更正等追溯调整或重述情况(如有)
			Element retroactive = companyPublicInfo.findNextByChar(env.getProperty("pdf.three.b"), nonrecurring, 2);
			if(retroactive!=null){
	//			retroactive = companyPublicInfo.findNextTag("table", retroactive);
				retroactive = retroactive.nextElementSibling();
				Elements retroactiveTr = retroactive.select("table tr");
				List<ReportRetroactive> reportRetroactiveList = new ArrayList<ReportRetroactive>();
				for (int i = 2; i < retroactiveTr.size(); i++) {
						ReportRetroactive reportRetroactive = new ReportRetroactive();
						Elements etds = retroactiveTr.get(i).children();
						if(etds.size()==7){
							reportRetroactive.setAcctName(etds.get(0).text());
							reportRetroactive.setThisPeriodBeforeRe(etds.get(1).text());
							reportRetroactive.setThisPeriodAfterRe(etds.get(2).text());
							reportRetroactive.setLastPeriodBeforeRe(etds.get(3).text());
							reportRetroactive.setLastPeriodAfterRe(etds.get(4).text());
		//					reportRetroactive.setPeriodBeforeLastBeforeRe(etds.get(5).text());
		//					reportRetroactive.setPeriodBeforeLastAfterRe(etds.get(6).text());
							reportRetroactive.setProjectScopeId(companyPtrId);
							reportRetroactiveList.add(reportRetroactive);
						}
				}
				reportRetroactiveRepositoy.save(reportRetroactiveList);
			}
		}
	}

}
